import { createRouter, createWebHistory } from 'vue-router'
import { isTokenExpired, removeToken } from '@/utils/auth'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/Register.vue')
    },
    // 游客模块路由
    {
      path: '/tourist',
      component: () => import('@/layouts/TouristLayout.vue'),
      children: [
        {
          path: 'home',
          name: 'TouristHome',
          component: () => import('@/views/tourist/Home.vue')
        },
        {
          path: 'products',
          name: 'Products',
          component: () => import('@/views/tourist/Products.vue')
        },
        {
          path: 'tourism',
          name: 'Tourism',
          component: () => import('@/views/tourist/Tourism.vue')
        },
        {
          path: 'ai-chat',
          name: 'AiChat',
          component: () => import('@/views/tourist/AiChat.vue')
        },
        {
          path: 'cart',
          name: 'Cart',
          component: () => import('@/views/tourist/Cart.vue')
        },
        {
          path: 'products/:id',
          name: 'ProductDetail',
          component: () => import('@/views/tourist/ProductDetail.vue')
        },
        {
          path: 'profile',
          name: 'TouristProfile',
          component: () => import('@/views/tourist/Profile.vue')
        },
        {
          path: 'attraction/:id',
          name: 'AttractionDetail',
          component: () => import('@/views/tourist/AttractionDetail.vue')
        }
      ]
    },
    // 农户模块路由
    {
      path: '/farmer',
      component: () => import('../layouts/FarmerLayout.vue'),
      children: [
        {
          path: 'home',
          name: 'FarmerHome',
          component: () => import('@/views/farmer/Home.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: 'products',
          name: 'FarmerProducts',
          component: () => import('@/views/farmer/Products.vue'),
          meta: {
            title: '农产品管理'
          }
        },
        {
          path: 'attractions',
          name: 'FarmerAttractions',
          component: () => import('@/views/farmer/Attractions.vue'),
          meta: {
            title: '乡村旅游景点'
          }
        },
        {
          path: 'ai-chat',
          name: 'FarmerAiChat',
          component: () => import('@/views/farmer/AiChat.vue'),
          meta: {
            title: 'AI助农'
          }
        },
        {
          path: 'orders',
          name: 'FarmerOrders',
          component: () => import('@/views/farmer/Orders.vue'),
          meta: {
            title: '订单管理'
          }
        },
        {
          path: 'profile',
          name: 'FarmerProfile',
          component: () => import('@/views/farmer/Profile.vue'),
          meta: {
            title: '个人中心'
          }
        }
      ]
    }
  ]
})

// 修改路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 检查token是否过期
  if (token && isTokenExpired()) {
    // token过期，清除token并跳转到登录页
    removeToken();
    ElMessage.error('登录已过期，请重新登录');
    next('/login');
    return;
  }
  
  // 修改这里，允许访问登录和注册页面
  if (to.path === '/login' || to.path === '/register') {
    if (token && !isTokenExpired()) {
      // 已登录且token未过期的用户访问登录或注册页面时重定向到首页
      next('/farmer/home')
    } else {
      // 未登录用户或token过期允许访问登录和注册页面
      next()
    }
  } else if (!token && to.path !== '/login' && to.path !== '/register') {
    // 未登录用户访问其他页面时重定向到登录页
    next('/login')
  } else {
    // 其他情况正常放行
    next()
  }
})

export default router 